package viewcomponents
{
	import com.adobe.ac.mxeffects.CubeRotate;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import fui.FUIControllers.UISession;
	import fui.FUIData.IDBClient;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.ViewStack;

	public class LoginRegistration extends  Canvas implements IComponent
	{
		private var _componentID:Number;
		private var _uiSession:UISession;
		private var _dbClient:IDBClient;
		private var login:Login = new Login();
		private var logout:Logout = new Logout();
		private var loginViewStack:ViewStack = new ViewStack();
		private var loginEffect:CubeRotate = new CubeRotate();
		private var logoutEffect:CubeRotate = new CubeRotate();
		
		private var isLogin:Boolean = true;
		
		/* <mx:ViewStack id="loginViewStack">
	
		<sides:Login 
			id="login" 
			title="Login" 
			hideEffect="{ flipIn }" 
			change="loginViewStack.selectedChild = registration;"/>
		<sides:Registration 
			id="registration" 
			title="Registration" 			
			hideEffect="{ flipBack }" 
			change="loginViewStack.selectedChild = login;"/>
	
	</mx:ViewStack>
 */
		
		public function LoginRegistration(session:UISession, client:IDBClient)
		{
			super();
			_uiSession = session;
			_dbClient = client;
			initEffects();
			addChild(loginViewStack);
			loginViewStack.addChild(login);
			login.setStyle("hideEffect", loginEffect);
			loginViewStack.addChild(logout);
			logout.setStyle("hideEffect", logoutEffect);
			login.loginButton.addEventListener(MouseEvent.CLICK, loginClick);
			
		}
		
		private function initEffects():void{
			loginEffect.target = login;
			loginEffect.siblings = [logout];
			loginEffect.direction = "RIGHT";
			loginEffect.duration = 1000;
			
			logoutEffect.target = logout;
			logoutEffect.siblings = [login];
			logoutEffect.direction = "LEFT";
			logoutEffect.duration = 1000;
		}
		
		public function loginClick(e:MouseEvent):void {
			
			_dbClient.addEventListener(Event.COMPLETE, ifAuthed);
			var str:String = "SELECT * FROM `users` WHERE `name` = '" + login.userInput.text + "' AND `passwd` = '" + login.passwInput.text + "'";
			_dbClient.CallStoredProcedure(str);
		}
		
		private function logoutClick(e:MouseEvent):void {
			//_loBtn.label = "Login";
			
			//login.loginButton.removeEventListener(MouseEvent.CLICK, logoutClick);
			_uiSession.dropSession();
			//_loBtn.addEventListener(MouseEvent.CLICK, LoginClick);
			//buildView();
			_uiSession.loginres(false);
			dispatchEvent(new Event("LOGGEDOUT"));
		}
		
		private function ifAuthed(event:Event):void{
			var results:ArrayCollection = _dbClient.results;
			trace (results);
			if(results && results.length > 0)
			{
				loginViewStack.selectedChild = logout;
				var obj:Object = results[0];
				logout.user.label = obj.name;
				_uiSession.startNewSession(obj.id, obj.name);
				_uiSession.profile = results;
				_uiSession.loginres(true);
				trace("######## profile assigned to uiSession ########");
				buildLoggedIn()
				dispatchEvent(new Event("LOGGEDIN"));
			}
			else
			{
				//login.failLogIn.text = "Failed to log in";	
			}
		}
		
		private function buildLoggedIn():void{
			//_mainPane.height = 115;
			
			
			//registration.userLogIn.label = "User " + login.loginButton.label + " \n is logged in!";	
			/* if(_warn != null)
				_mainPane.removeChild(_warn);
			_loBtn.label = "Logout"; */
			//login.loginButton.removeEventListener(MouseEvent.CLICK, loginClick);
			//login.loginButton.addEventListener(MouseEvent.CLICK, logoutClick);
		}
		
		
		public function set componentID(value:Number):void{
			if(!_componentID){
				_componentID = value;
			}
		}
		
		public function get componentID():Number
		{
			return 0;
		}
		
		public function set xmlSource(xml_path:String):void
		{
		}
		
		public function set session(uis:UISession):void
		{
		}
		
		public function get componentTitle():String
		{
			return null;
		}
		
		public function showComponent():void
		{
		}
		
		public function hideComponent():void
		{
		}
		
		public function isNeedAuth():Boolean
		{
			return false;
		}
		
		public function selfRemove():void
		{
		}
		
	}
}